package com.xnyzc.lhy.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.parameters.P;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 检测
 */
public class CheckUtil {

    /**
     * 判断对象是否全部为空, 全为空则返回true
     *
     * @param objs
     * @return
     */
    public static boolean allObjIsEmpty(Object... objs) {
        if (objs == null || objs.length == 0) {
            return true;
        }
        int flag = 0;
        for (Object obj : objs) {
            if (Objects.isNull(obj)) {
                flag++;
                continue;
            } else if (obj instanceof String && strIsEmpty(obj.toString())) {
                flag++;
            } else if (obj instanceof Collection && collectionIsEmpty((Collection) obj)) {
                flag++;
            } else if (obj instanceof Map && mapIsEmpty((Map) obj)) {
                flag++;
            }
        }
        if(flag == objs.length){
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否为空, 有一个为空则返回true
     *
     * @param strs
     * @return
     */
    public static boolean strIsEmpty(String... strs) {
        if (strs == null || strs.length == 0) {
            return true;
        }

        for (String str : strs) {
            if (str == null || str.trim().length() == 0) {
                return true;
            }
        }

        return false;
    }

    /**
     * 判断字符串非空, 有一个为空返回false
     *
     * @return
     */
    public static boolean strIsNotEmpty(String... strs) {
        if (strs == null || strs.length == 0) {
            return false;
        }

        for (String str : strs) {
            if (str == null || str.trim().length() == 0) {
                return false;
            }
        }

        return true;

    }

    /**
     * 判断对象是否为空, 有一个为空则返回true
     *
     * @param objs
     * @return
     */
    public static boolean objIsEmpty(Object... objs) {
        if (objs == null || objs.length == 0) {
            return true;
        }

        for (Object obj : objs) {
            if (Objects.isNull(obj)) {
                return true;
            } else if (obj instanceof String && strIsEmpty(obj.toString())) {
                return true;
            } else if (obj instanceof Collection && collectionIsEmpty((Collection) obj)) {
                return true;
            } else if (obj instanceof Map && mapIsEmpty((Map) obj)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 判断对象非空, 有一个为空返回false
     *
     * @param objs
     * @return
     */
    public static boolean objIsNotEmpty(Object... objs) {
        if (objs == null || objs.length == 0) {
            return false;
        }

        for (Object obj : objs) {
            if (Objects.isNull(obj)) {
                return false;
            } else if (obj instanceof String && strIsEmpty(obj.toString())) {
                return false;
            } else if (obj instanceof Collection && collectionIsEmpty((Collection) obj)) {
                return false;
            } else if (obj instanceof Map && mapIsEmpty((Map) obj)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 判断集合对象是否为空
     *
     * @param cs
     * @return
     */
    public static boolean collectionIsEmpty(Collection... cs) {
        if (cs == null || cs.length == 0) {
            return true;
        }

        for (Collection c : cs) {
            if (c == null || c.size() <= 0) {
                return true;
            }
        }

        return false;
    }

    /**
     * 判断集合对象是否不为空
     *
     * @param cs
     * @return
     */
    public static boolean collectionIsNotEmpty(Collection... cs) {
        if (cs == null || cs.length == 0) {
            return false;
        }

        for (Collection c : cs) {
            if (c == null || c.size() <= 0) {
                return false;
            }
        }

        return true;
    }

    /**
     * 判断Map对象是否为空
     *
     * @param ms
     * @return
     */
    public static boolean mapIsEmpty(Map... ms) {
        if (ms == null || ms.length == 0) {
            return true;
        }

        for (Map m : ms) {
            if (m == null || m.size() <= 0) {
                return true;
            }
        }

        return false;
    }

    /**
     * 判断Map对象是否不为空
     *
     * @param ms
     * @return
     */
    public static boolean mapIsNotEmpty(Map... ms) {
        if (ms == null || ms.length == 0) {
            return false;
        }

        for (Map m : ms) {
            if (m == null || m.size() <= 0) {
                return false;
            }
        }

        return true;
    }


    /**
     * 判断数组对象是否为空
     *
     * @param os
     * @return
     */
    public static boolean arrayIsEmpty(Object[]... os) {
        if (os == null || os.length == 0) {
            return true;
        }

        for (Object[] o : os) {
            if (o == null || o.length == 0) {
                return true;
            }
        }

        return false;
    }


    /**
     * 判判断数组对象是否不为空
     *
     * @param os
     * @return
     */
    public static boolean arrayIsNotEmpty(Object[]... os) {
        if (os == null || os.length == 0) {
            return false;
        }

        for (Object[] o : os) {
            if (o == null || o.length == 0) {
                return false;
            }
        }

        return true;
    }

    /**
     * 判断json是否正确
     *
     * @param jsonInString
     * @return
     */
    public final static boolean isJson(String jsonInString) {
        try {
            final ObjectMapper mapper = new ObjectMapper();
            mapper.readTree(jsonInString);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public final static boolean isNumber(Integer... num) {
        if (num == null || num.length == 0) {
            return false;
        }
        for (Integer integer : num) {
            if (integer == null) {
                return false;
            }
        }
        return true;
    }

    public final static boolean numberItZero(Integer num) {
        if (num == null || num == 0) {
            return false;
        }
        return true;
    }

    /**
     * 是否是汉字
     * @param countname
     * @return
     */
    public static boolean isUtf8(String countname){
        String p = "[\u4e00-\u9fa5]";
        Pattern pattern = Pattern.compile(p);
        Matcher m = pattern.matcher(countname);
        if (m.find()) {
            return true;
        }
        return false;
    }
}
